﻿CREATE procedure dbo.CreateTakeoutUser
(
		@TakeoutProviderID int,
		@UserID int,
		@Instructions varchar(2000),
		@MenuItems varchar(1000), /* CSV of ID,Quantity */
		@TakeoutUserID int = null OUTPUT
	)

AS
	
	DECLARE @Pos int
	DECLARE @MenuItemID int
	DECLARE @Quantity int
	DECLARE @OrderTotal money
	DECLARE @ItemPrice money
	DECLARE @TaxRate decimal(4,2)
	DECLARE @MenusID int
	
	BEGIN TRANSACTION
	
		/* First.  Create the takeout user */
		/* Modified Feb 20, 2005 - CJT - If TakeoutUserID is passed in don't insert the record*/
		
		if @TakeoutUserID = 0 
		Begin
			INSERT INTO dbo.TakeoutUser
		                      (TakeoutProviderID, UserID, IsPaid, Instructions, OrderTime, OrderTotal)
			VALUES     (@TakeoutProviderID,@UserID, 0,@Instructions, GETDATE(), 0)
			
			SET @TakeoutUserID = SCOPE_IDENTITY()
			
			UPDATE dbo.TakeoutProvider 
			SET MaximumOrders = MaximumOrders - 1
			WHERE Id = @TakeoutProviderID
		END
			
		if isnull(@TakeoutUserID,0) = 0
		Begin
			INSERT INTO dbo.TakeoutUser
		                      (TakeoutProviderID, UserID, IsPaid, Instructions, OrderTime, OrderTotal)
			VALUES     (@TakeoutProviderID,@UserID, 0,@Instructions, GETDATE(), 0)
			
			SET @TakeoutUserID = SCOPE_IDENTITY()
			
			UPDATE dbo.TakeoutProvider 
			SET MaximumOrders = MaximumOrders - 1
			WHERE Id = @TakeoutProviderID
		END
		
		/* Second. Parse the order items and insert */
		
		SET @MenuItems = LTRIM(RTRIM(@MenuItems))+ ','
		SET @Pos = CHARINDEX(',', @MenuItems, 1)

		IF REPLACE(@MenuItems, ',', '') <> ''
			BEGIN
			
			/* Kill out All order items */
			
			DELETE FROM dbo.OrderItems
			WHERE     (TakeoutUserId = @TakeoutUserID)
			
				WHILE @Pos > 0
					BEGIN
			
						
						Set @MenuItemID = LTRIM(RTRIM(LEFT(@MenuItems, @Pos -1)))
									
						SET @MenuItems = RIGHT(@MenuItems, LEN(@MenuItems) - @Pos)
						SET @Pos = CHARINDEX(',', @MenuItems, 1)
			
						SET @Quantity = LTRIM(RTRIM(LEFT(@MenuItems, @Pos - 1)))
			
						SET @MenuItems = RIGHT(@MenuItems, LEN(@MenuItems) - @Pos)
						SET @Pos = CHARINDEX(',', @MenuItems, 1)
						
						
						IF @MenusID = 0
							BEGIN
								SET @MenusID = (SELECT     dbo.Menus.Id
								               FROM         dbo.Menus INNER JOIN
								                                     dbo.MenuItems ON dbo.Menus.Id = dbo.MenuItems.MenusId
								               WHERE     (dbo.MenuItems.Id = @MenuItemID))
							END
		
						SET @ItemPrice = (SELECT Price FROM dbo.MenuItems WHERE Id = @MenuItemID)
						SET @TaxRate = (SELECT (TaxRate / 100) as TaxRate FROM dbo.Menus WHERE Id = @MenusID)
						
							
						INSERT INTO dbo.OrderItems
						                      (TakeoutUserId, MenuItemID, Quantity)
						VALUES     (@TakeoutUserID,@MenuItemID,@Quantity)
						
					END
					
					/* Inject the Order Total into the Takeout User */
		
					SET @OrderTotal = (
						SELECT     ROUND(SUM((1 + (dbo.Menus.TaxRate / 100)) * (dbo.MenuItems.Price * dbo.OrderItems.Quantity)), 2) AS OrderTotal
						FROM         dbo.OrderItems INNER JOIN
			                      dbo.MenuItems ON dbo.OrderItems.MenuItemID = dbo.MenuItems.Id INNER JOIN
			                      dbo.Menus ON dbo.MenuItems.MenusId = dbo.Menus.Id
						WHERE     (dbo.OrderItems.TakeoutUserId = @TakeoutUserId))
		
					SET @OrderTotal = @OrderTotal + 
						(SELECT     TakeoutCharge
						 FROM         dbo.TakeoutProvider
						 WHERE     (Id = @TakeoutProviderID) )

					UPDATE    dbo.TakeoutUser
						SET              OrderTotal = @OrderTotal
						WHERE     (Id = @TakeoutUserID) 
					
					
			END	

		
		
		
		
	if @@ERROR = 0	
		BEGIN
			COMMIT TRANSACTION
		END
	ELSE	
		BEGIN
			ROLLBACK TRANSACTION
		END
	
	RETURN


